﻿using System;
using AutoRunCard;
using NDK.Module.Model;
using BZ.Logger;

namespace NDK.Utils
{
    class SMPMesUpload
    {
        private static log4net.ILog m_Logger = log4net.LogManager.GetLogger("SMPMesUpload");

        /// <summary>
        /// AE/TE上傳資料前的檢查
        /// </summary>
        /// <param name="strSiteName">站別(APMT)</param>
        /// <param name="strSn">SMP碼（不為空）</param>
        /// <param name="strSnClass">條碼類別（SN）（不為空） </param>
        /// <param name="strProgramName">程序名（不為空）</param>
        /// <param name="strErrorCode">MES返回的錯誤代碼</param>
        /// <param name="strErrorMsg">MES返回錯誤代碼對應的具體描述</param>
        /// <param name="strActionCode">MES返回的幾台狀態（G:正常，Y:警告，R:停機）</param>
        /// <param name="strActionMsg">MES返回幾台狀態對應的具體描述</param>
        /// <returns>True:允許進站，查看strActionCode，strActionMsg；False不允許進站，需要查看strErrorCode, strErrorMsg </returns>
        //public bool InsiteCheck(string strSiteName, string strSn, string strSnClass, string strProgramName, 
        //out string strErrorCode, out string strErrorMsg, out string strActionCode, out string strActionMsg)

        public static bool SMPInsiteCheck(string strSn)
        {
            string sSiteName = "APMT";
            //string sSn = "SMPSNtest";
            string sSnClass = "SN";
            string sProgramName = "BZMeasurement_" + Common.currentVersion;

            string sErrCode = "";
            string sErrMsg = "";
            string sActionCode = "";
            string sActionMsg = "";
            bool bResult = false;
            IRunCard ainfo = new RunCard();
            bResult = ainfo.InsiteCheck(sSiteName, strSn, sSnClass, sProgramName, out sErrCode, out sErrMsg, out sActionCode, out sActionMsg);
            if (bResult == true)
            {
                Common.AppendUiLog.Log(strSn + ":在当前APMT工站排队，允许测试上传" + sActionCode + "：" + sActionMsg, LogType.Alert);
                return true;
            }
            else
            {
                Common.AppendUiLog.Log(strSn + ":不在当前APMT工站，不允许测试上传，错误代码：" + sErrCode + "：" + sErrMsg, LogType.Error);
                return false;
            }
        }


        /// <param name=" strSubLine ">线别DB25</param>
        /// <param name=" sStationCode ">站点类别码（APMT） </param>
        /// <param name=" sRecordingTime ">记录时间 </param>
        /// <param name=" sErrorCode ">不良代码""</param>
        /// <param name=" sLeftRight ">左右 </param>
        /// <param name=" sTestResult ">测试结果 </param> 格式正确为OK 失败为NG 可传入错误代码
        /// <param name=" sCarrierBarcode ">Carrier码""</param>
        /// <param name=" sCellBarcode ">cell码""</param>
        /// <param name=" sSmpCode ">SMP代码FG</param>
        /// <param name=" sReworkable ">是否重工""</param>
        /// <param name=" sProVer ">程式版本 </param>
        /// <param name=" ArryParams ">空数据 </param>
        /// <param name=" StrMessage ">返回信息 </param>
        /// <returns>true:成功 false：失败 </returns>
        //线别,站点类别码,记录时间 ,测试结果 ,SMP代码,为必要参数一定要传
        //public bool Data_Collection_Auto(string strSubLine, string sStationCode, DateTime sRecordingTime, string sErrorCode, string sLeftRight, string sTestResult, string sCarrierBarcode, string sCellBarcode, string sSmpCode, string sReworkable, string sProVer, string[] ArryParams, out string StrMessage)

        public static bool SMPMesData_Collection_Auto(MeasureProduct battery, string[] ArryFAIParam)
        {
            string strSubLine = "DB25";
            string sStationCode = "ACMT";
            DateTime sRecordingTime = DateTime.Now;
            string sErrorCode = battery.NG_FAI;
            string sLeftRight = battery.Position;
            string sTestResult = battery.OK_NG;
            string sCarrierBarcode = "Null";
            string sCellBarcode = "Null";
            string sSmpCode = battery.SN;
            string sReworkable = "Empty";
            string sProVer = "NDKMeasurement_" + Common.currentVersion;
            string StrMessage = "";

            bool flag = false;
            RunCard run = new RunCard();
            try
            {
                flag = run.Data_Collection_Auto(strSubLine, sStationCode, sRecordingTime, sErrorCode, sLeftRight, sTestResult, sCarrierBarcode,
                                            sCellBarcode, sSmpCode, sReworkable, sProVer, ArryFAIParam, out StrMessage);

                m_Logger.Error(strSubLine + " || " + sStationCode + " || " + sRecordingTime + " || " + sErrorCode + " || " + sLeftRight + " || " + sTestResult
                + " || " + sCarrierBarcode + " || " + sCellBarcode + " || " + sSmpCode + " || " + sReworkable + " || " + sProVer + " || " + ArryFAIParam.ToString());

            }
            catch (Exception xxx)
            {
                m_Logger.Error(xxx.StackTrace);
            }

            if (flag == true)
            {
                Common.AppendUiLog.Log("数据上传Mes成功：" + StrMessage, LogType.Alert);
                return true;
            }
            else
            {
                Common.AppendUiLog.Log("数据上传Mes失败，错误代码：" + StrMessage, LogType.Error);
                return false;
            }
        }
    }
}
